package com.shxy.common.entity;

import com.shxy.common.enums.ResultCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 自定义返回结果
 */
@Data
public class CustomResult<T> implements Serializable {
    private int code = 200;
    private String message = "";
    private T data;

    public CustomResult() {}

    public CustomResult(int code) {
        this.code = code;
        this.message = code == 200 ? ResultCode.SUCCESS.getMessage() : ResultCode.COMMON_FAIL.getMessage();
    }

    public CustomResult(int code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public CustomResult(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }

    public CustomResult(ResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }

    public CustomResult(T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.data = data;
    }

    /***********创建结果对象**********************/
    public static CustomResult create(ResultCode resultCode) {
        return new CustomResult(resultCode);
    }

    public static CustomResult create(int code) {
        return new CustomResult(code);
    }

    public static CustomResult create(int code, String msg, Object data){
        return new CustomResult(code, msg, data);
    }

    public static CustomResult create(Object data) {
        return new CustomResult(data);
    }
}
